import java.util.Arrays;
import java.util.Random;

class Help {
    long oddNumSum;
    long evenNumSum;
    public void oddSum (long odd){
        oddNumSum += odd;
    }
    public void evenSum (long even){
        evenNumSum += even;
    }
    public void show () {
        System.out.println("偶数之和:" + oddNumSum);
        System.out.println("奇数之和:" + evenNumSum);
        System.out.println("奇数加偶数之和:" + (evenNumSum+oddNumSum) );
    }
}

public class Demo6 {
    public static final int N = 1000_0000;

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();

        int[] arr = new int[N];
        for(int i = 0; i < N; i++){
            arr[i] = (int) (Math.random()*100);
        }
        //System.out.println(Arrays.toString(arr));
        // 构造随机数，填充数组
//        Random random = new Random();
//        for (int i = 0; i < N; i++) {
//            int num = random.nextInt(100) + 1;
//            arr[i] = num;
//        }

        Help help = new Help();

        Thread t1 = new Thread(()->{
            for (int i = 0; i < N; i += 2) {
                help.oddSum(arr[i]);
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 1; i < N; i += 2) {
                help.evenSum(arr[i]);
            }
        });
        t1.start();
        t2.start();
        // 等待线程结束
        t1.join();
        t2.join();
        help.show();
        System.out.print("程序用了:" + (System.currentTimeMillis() - start) + "ms");
    }
}
